<template>
    <n-config-provider style="height: 100%;" :locale="zhCN" :date-locale="dateZhCN">
        <n-loading-bar-provider>
            <n-dialog-provider>
                <n-notification-provider>
                    <n-message-provider>
                        <router-view></router-view>
                        <naive-provider-content/>
                    </n-message-provider>
                </n-notification-provider>
            </n-dialog-provider>
        </n-loading-bar-provider>
    </n-config-provider>
</template>

<script setup>
import {
    useLoadingBar,
    useDialog,
    useMessage,
    useNotification,
    NDialogProvider,
    NConfigProvider,
    NLoadingBarProvider,
    NMessageProvider,
    NNotificationProvider
} from 'naive-ui'
import {zhCN, dateZhCN} from 'naive-ui'
import {defineComponent, h, onMounted} from "vue";


// 挂载naive组件的方法至window, 以便在路由钩子函数和请求函数里面调用
function registerNaiveTools() {
    window.$loadingBar = useLoadingBar()
    window.$dialog = useDialog()
    window.$message = useMessage()
    window.$success = message('success')
    window.$warning = message('warning')
    window.$error = message('error')
    window.$info = message('info')
    window.$notification = useNotification()
}

function message(
    type
) {
    return (content, duration = 1000, options) => {
        return new Promise((resolve) => {
            window.$message?.[type](content, {
                duration,
                ...options,
                onLeave: () => {
                    if (options?.onLeave) {
                        options.onLeave()
                    }
                    resolve()
                }
            })
        })
    }
}


const NaiveProviderContent = defineComponent({
    name: 'NaiveProviderContent',
    setup() {
        // console.debug(111)
        registerNaiveTools()
    },
    render() {
        return h('div')
    }
})

</script>

<style>
._container {
    box-sizing: border-box;
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: #fff;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: v-sans, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

html, body, #app {
    height: 100%;
    width: 100%;
}

.h-full {
    height: 100%;
}

.w-full {
    width: 100%;
}

.flex {
    display: flex;
}

.flex-col {
    display: flex;
    flex-direction: column;
}

.flex-1 {
    flex: 1;
}

.justify-center {
    justify-content: center;
}

.justify-end {
    justify-content: end;
}

.justify-between {
    justify-content: space-between;
}

.items-center {
    align-items: center;
}

.hidden {
    display: none;
}

.mx-8px {
    margin-left: 8px;
    margin-right: 8px;
}

</style>
